<?php
/**
 * Created by PhpStorm.
 * User: testypp
 * Date: 2017/12/18
 * Time: 下午2:38
 */

// 载入取名字的类
require ('rndChinaName.php');

// User API 总入口
class Api_User extends PhalApi_Api {

    // 每个方法的参数设定
    public function getRules() {
        return array(
            'getUserByMobile' => array(
                'mobile' => array('name' => 'mobile', 'min' => 1, 'require' => true, 'desc' => '用户手机号'),
            ),
            'getUserByID' => array(
                'userid' => array('name' => 'userid', 'min' => 1, 'require' => true, 'desc' => '用户ID')
            ),
            'addUser' => array(
                'mobile' => array('name' => 'mobile', 'min' => 1, 'require' => true, 'desc' => '用户手机号'),
                'password' => array('name' => 'password', 'min' => 1, 'require' => true, 'desc' => '用户密码'),
                'nickname' => array('name' => 'nickname', 'min' => 1, 'require' => true, 'desc' => '用户昵称')
            ),
            'loginUser' => array(
                'mobile' => array('name' => 'mobile', 'min' => 1, 'require' => true, 'desc' => '用户手机号'),
                'password' => array('name' => 'password', 'min' => 1, 'require' => true, 'desc' => '用户密码')
            ),
        );
    }

    // getUserByMobile
    public function getUserByMobile() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $domain = new Domain_User();

        $info = $domain->getUserByMobile($this->mobile);

        if (empty($info)) {
            DI()->logger->debug('用户不存在', $this->mobile);

            $rs['code'] = 1;
            $rs['msg'] = T('用户不存在');
            return $rs;
        }

        $rs['info'] = $info;

        return $rs;
    }

    // getUserByID
    public function getUserByID() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $domain = new Domain_User();

        $info = $domain->getUserByID($this->userid);

        if (empty($info)) {
            DI()->logger->debug('用户不存在', $this->userid);

            $rs['code'] = 1;
            $rs['msg'] = T('用户不存在');
            return $rs;
        }

        $rs['info'] = $info;

        return $rs;
    }

    // addUser
    public function addUser() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $domain = new Domain_User();

        $info = $domain->addUser($this->mobile, $this->password, $this->nickname);

        if (empty($info)) {
            DI()->logger->debug('注册失败', $this->mobile);

            $rs['code'] = 1;
            $rs['msg'] = T('注册失败');
            return $rs;
        }

        $rs['info'] = $info;

        return $rs;
    }

    // loginUser
    public function loginUser() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $domain = new Domain_User();

        $info = $domain->loginUser($this->mobile, $this->password);

        if (empty($info)) {
            DI()->logger->debug('用户不存在', $this->mobile);

            $rs['code'] = 1;
            $rs['msg'] = T('用户不存在');
            return $rs;
        }

        $rs['info'] = $info;

        return $rs;
    }

    // getRandomName
    public function getRandomName() {

        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        $name_obj = new rndChinaName();
        $name = $name_obj->getName(2);

        $rs['info'] = $name;

        return $rs;
    }


}